<template>
	<view class="container">
		<view class="title">
			设置新密码
		</view>
		<view class="marked">
			新密码至少8个字符，不能全是字母或数字
		</view>
		<view class="list">
			<!-- <view class="item">
				<view class="top flexCenter">
					<image
						src="@/static/images/mine/phone.png"
						mode=""
					></image>
					手机号
				</view>
				<view class="bottom flexBetween">
					{{ form.mobile }}
					<view
						class="button"
						@tap="sendSms"
					>
						发送验证码
					</view>
				</view>
			</view>
			<view class="item">
				<view class="top flexCenter">
					<image
						src="@/static/images/mine/yzm.png"
						mode=""
					></image>
					验证码
				</view>
				<view class="bottom flexBetween">
					<input
						type="number"
						placeholder="请输入手机验证码"
						v-model="form.smscode"
					>
				</view>
			</view> -->
			<view class="item">
				<view class="top flexCenter">
					<image
						src="@/static/images/mine/pwd.png"
						mode=""
					></image>
					新密码
				</view>
				<view class="bottom flexBetween">
					<input
						type="text"
						:password="true"
						placeholder="请输入新密码"
						v-model="form.newpassword"
					>
				</view>
			</view>
			<view class="item">
				<view class="top flexCenter">
					<image
						src="@/static/images/mine/pwd.png"
						mode=""
					></image>
					确认密码
				</view>
				<view class="bottom flexBetween">
					<input
						type="text"
						:password="true"
						placeholder="请再次输入新密码"
						v-model="form.newpassword2"
					>
				</view>
			</view>
		</view>
		<button
			class="submit"
			@tap="query"
		>提交</button>
	</view>
</template>

<script>
import { Login, Home } from '@/config/API.js'
import { toast,logout } from '@/utils/uni'
export default {
	data () {
		return {
			form: {
				mobile: '',
				newpassword: '',
				smscode: ''
			}
		};
	},
	mounted () {
		this.getUserInfo()
	},
	methods: {
		// 获取用户信息
		getUserInfo () {
			Home.userCenter().then(res => {
				let data = res.data
				for (let key of Object.keys(this.form)) {
					this.form[key] = data[key] || ''
				}
			})
		},
		// 发送验证码
		sendSms () {
			Login.sendSms({ mobile: this.form.mobile }).then(res => {
				toast(res.msg)
			})
		},
		logout,
		// 修改密码
		query () {
			Home.userCenter().then(res => {
				this.form.mobile =res.data.username
			})
			
			// if (!this.form.smscode) {
			// 	toast('请输入手机验证码')
			// 	return
			// } else 
			if (!this.form.newpassword) {
				toast('请输入新密码')
				return
			} else if (!this.form.newpassword2) {
				toast('请再次输入新密码')
				return
			} else if (this.form.newpassword != this.form.newpassword2) {
				toast('请确认两次输入密码一致')
				return
			}
			Login.changePassword(this.form).then(res => {
				toast('修改成功')
				this.logout()
				setTimeout(() => {
					uni.navigateBack()
				}, 1000)
			})
		},

	}
}
</script>

<style lang="scss" scoped>
.container {
	padding: 0 48rpx;
}

.title {
	font-size: 60rpx;
	font-family: PingFang SC-Bold, PingFang SC;
	font-weight: bold;
	color: #272729;
	margin-top: 50rpx;
}
.marked {
	font-size: 26rpx;
	font-family: PingFang SC-Medium, PingFang SC;
	font-weight: 500;
	color: #666666;
}
.list {
	margin-top: 47rpx;
	.item {
		.top {
			margin-top: 43rpx;
			margin-bottom: 18rpx;

			font-size: 30rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #272729;

			image {
				width: 36rpx;
				height: 36rpx;
				margin-right: 11rpx;
			}
		}
		.bottom {
			margin-top: 33rpx;
			height: 76rpx;
			line-height: 76rpx;
			font-size: 30rpx;
			font-family: PingFang SC-Bold, PingFang SC;
			font-weight: bold;
			color: #333333;
			border-bottom: 1px solid #e1e1e6;
			.button {
				width: 176rpx;
				height: 64rpx;
				line-height: 64rpx;
				text-align: center;
				background: linear-gradient(270deg, #ff0000 0%, #fe5454 100%);
				border-radius: 70rpx 70rpx 70rpx 70rpx;

				font-size: 24rpx;
				font-family: PingFang SC-Regular, PingFang SC;
				font-weight: 400;
				color: #ffffff;
			}
		}
	}
}
.submit {
	margin-top: 83rpx;
}
</style>